package tech.aistar.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import tech.aistar.pojo.Cart;
import tech.aistar.pojo.Result;
import tech.aistar.pojo.User;
import tech.aistar.service.UserService;
import tech.aistar.service.UserServiceImpl;
import tech.aistar.util.JWTUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;

//@Controller
//@ResponseBody
@RestController // = @ResponseBody + @Controller
@RequestMapping("user")
public class UserController {
    //controller层调用service
    @Autowired
    private UserService userService;

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("index")
    public Result index(HttpServletRequest request){
        Integer id = (Integer) request.getAttribute("id");
        User user = userService.findById(id);
        return new Result("200","登录用户的信息!",user);
    }


    @GetMapping
    public Result sendCode(String email){
        User user = userService.findByEmail(email);
        if(user==null)
            return new Result("404","sorry,邮箱不存在!");

        //TODO... 发送邮箱的这段代码 - 单独使用一个线程单独去处理
        //说明邮箱是存在的
        //随机数100000~999999之间随机正整数
        //java.lang.Math  static double random();//[0.0,1.0)
        int n = (int) (Math.random()*900000+100000);
        //将int类型转换成字符串类型
        String serverCode = String.valueOf(n);

        //1. 定义一个对象,封装邮件的信息
        SimpleMailMessage msg = new SimpleMailMessage();
        //邮件的发送方,此处的邮箱应该和application-dev.yml中的配置的mail下的username保持一致
        msg.setFrom("849962874@qq.com");
        //邮件的标题
        msg.setSubject("city-login验证码");
        //邮件的正文内容
        msg.setText(serverCode);
        //邮件的接收方
        msg.setTo(email);

        //TODO... 不让发送
        //真正发送
        //mailSender.send(msg);

        //TODO... 选择将serverCode放入到服务器的内存中session空间
        //①获取session对象
        //HttpSession session = request.getSession();
        //存储的数据的格式也是key-value的形式
        //session.setAttribute("serverCode",serverCode);

        //将验证码放入到redis中,并且设置过期时间
        redisTemplate.opsForValue().set(email,serverCode,40, TimeUnit.MINUTES);

        //TODO... 验证码不要返回
        return new Result("200","发送验证码成功",serverCode);
    }

    @PostMapping
    public Result login(String email,String code){
        User user = userService.findByEmail(email);
        if(user==null)
            return new Result("404","sorry,邮箱不存在!");

        //根据key,从session空间获取之前生成的验证码
//        HttpSession session = request.getSession();
//        String serverCode = (String) session.getAttribute("serverCode");

        //TODO 从redis中获取
        String serverCode = (String) redisTemplate.opsForValue().get(email);

        if(null!=serverCode && serverCode.equals(code)){
            //TODO... 在server端生成token
            String token = JWTUtils.geneJsonWebToken(user);
            //TODO.. 将这个token返回给客户端
             return new Result("200","login成功!",token);
        }

        return new Result("500","登录失败!验证码不正确或者过期了!");
    }
}
